﻿using Authority.DataTransferObject.ActionAuthority;
using Authority.DataTransferObject.MenuAuthority;
using Authority.DataTransferObject.Role;
using Authority.DataTransferObject.Routing;
using Authority.DataTransferObject.User;
using Authority.Doamin;
using Authority.Doamin.View;
using Authority.IServices.Model.Action;
using AutoMapper;
using HelperFromTheSkeleton.Common;
using HelperFromTheSkeleton.Convert;
using HelperFromTheSkeleton.ExtendMethod;

namespace Authority.ServiceImpls.Profile
{
    public class UserProfile : AutoMapper.Profile
    {
        public UserProfile()
        {
            CreateMap<User, UserListDTO>();
            CreateMap<UserActionInfoView, UserActionInfoDTO>()
                .ForMember(m => m.UserActionInfo, s => s.MapFrom(n => n.ActionInfo.Select(c => new ActionAuthoritytDTO
                {
                    ID = c.Key,
                    Code = c.Value.code,
                    Name = c.Value.name,
                    SubsystemID = c.Value.subsystemID
                }).ToList()));
            CreateMap<UserMenuInfoView, UserMenuListDTO>()
                .ForMember(m => m.UserMenuInfos, s => s.MapFrom(n => n.MenuInfos.Select(c => new ActionAuthoritytDTO
                {
                    ID = c.ID,
                    Code = c.Code,
                    Name = c.Name,
                    SubsystemID = c.SubsystemID
                }).ToList()));
            CreateMap<RoutingView, RoutingAuthorityDTO>();

            CreateMap<UserRoutingView, UserRoutingListDTO>()
                .ForMember(m => m.UserRoutingInfos, s => s.MapFrom(n => GetRoutingAuthorityList(n.RoutingsInfos)));

            CreateMap<UserRoleInfoView, UserRoleListDTO>()
                .ForMember(m => m.UserRoleInfos, s => s.MapFrom(n => n.RoleInfos.Select(c => new RoleAuthorityListDTO
                {
                    ID = c.ID,
                    Code = c.Code,
                    Name = c.Name
                })));
            CreateMap<UserMenuInfoView, UserMenuTreeDTO>()
                .ForMember(m => m.UserMenuTree, s => s.MapFrom(n => TreeConvert.EntityToTreeList<MenuTreeDTO, MenuView>(n.MenuInfos)));

            CreateMap<UserRoutingView, UserRoutingTreeDTO>()
                .ForMember(m => m.UserRoutingTree, s => s.MapFrom(n => TreeConvert.EntityToTreeList<RoutingTreeDTO, RoutingView>(n.RoutingsInfos)));
        }

        private List<RoutingAuthorityDTO> GetRoutingAuthorityList(List<RoutingView> model)
        {
            var result = model.Select(m =>
            {
                var n = new RoutingAuthorityDTO();
                m.CopyProperty(n);
                return n;
            }).ToList();
            return result;
        }
    }
}
